package com.mes.web.controller.hy;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.mes.entity.hy.Equipment;
import com.mes.entity.hy.Work;
import com.mes.entity.hy.Work1;
import com.mes.entity.jy.Productroom;
import com.mes.service.hy.IEquipmentService;
import com.mes.service.hy.IWorkService;
import com.mes.service.jy.CarRoomServise;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;

/**
 * 
 * @ClassName:QualityController
 * @Description: 品质管理controller层
 * @Author hy
 * @Date 2018-5-30下午3:58:28
 * @version 1.0
 */
@Controller
@RequestMapping("/quality")
public class QualityController {
	
	@Resource
	private IEquipmentService iEquipmentService;
	
	@Resource
	private IWorkService iWorkService;
	
	@Resource
	private CarRoomServise carRoomServise;
	
	/**
	 * 设备查询，根据车间查找设备
	 * @param response
	 * @param limit 每页显示行数
	 * @param offset 页码
	 * @param equipment 设备实体类
	 * @throws Exception
	 */
	@RequestMapping("/equipment")
	public void quipment(HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset, Equipment equipment)
			throws Exception {
		if (!"".equals(offset)) {
			equipment.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
		}
		equipment.setPageSize(Integer.parseInt(limit));
		
		List<Equipment> equipmentList = iEquipmentService.selectEquipment(equipment);
		int total = iEquipmentService.selectCount(equipment);
		
		JSONArray jsonArray = JSONArray.fromObject(equipmentList);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 品质查询
	 * @param response
	 * @param work 品质实体类
	 * @throws Exception
	 */
	@RequestMapping("/work")
	public void work(HttpServletResponse response,Work work) throws Exception{
		//一个设备会生产有两种产品，这里分左右两次分别查询，都放在list中，该list中的泛型类是页面显示用
		List<Work1> workList = new ArrayList<>();
		if((iWorkService.selectWork(work)) != null && !("").equals(iWorkService.selectWork(work).getNmum())){
			workList.add(iWorkService.selectWork(work));
		}
	/*	if(iWorkService.selectWorkr(work) != null && !("").equals(iWorkService.selectWorkr(work).getNmum())){
			workList.add(iWorkService.selectWorkr(work));
		}*/
		//查询不良品时条件多加了一个品编，所以创建一个类
		Work1 work2 = new Work1();
		for(int i = 0;i < workList.size();i++){
			if(workList.get(i).getNmum() != null && !("").equals(workList.get(i).getNmum())){
				//将list中查到的品编放入实体类中的一个备用字段中，留作下一步查询用
				work.setRemartk15(workList.get(i).getNmum());
				//查询不良品
				work2 = iWorkService.selectNGood(work);
				if(work2 != null){
					workList.get(i).setBrightMark(work2.getBrightMark());
					workList.get(i).setColdMetal(work2.getColdMetal());
					workList.get(i).setDeckleEdge(work2.getDeckleEdge());
					workList.get(i).setnForming(work2.getnForming());
				}
			}
		}
		
		JSONArray jsonArray = JSONArray.fromObject(workList);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 查询车间
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/selworkshop")
	public void selworkshop(HttpServletResponse response) throws Exception{
		List<Productroom> list = carRoomServise.selectCarRoomAll();
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,
				new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("data", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
}
